package com.jilefojing.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.pojos.RSourceUser;
import com.jilefojing.beans.pojos.base.BasePo;
import com.jilefojing.beans.pojos.enhanced.EnhancedRSourceUser;
import com.jilefojing.beans.pojos.enhanced.EnhancedUser;
import com.jilefojing.beans.pojos.enhanced.base.BaseEnhanced;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.IRSourceUserDao;
import com.jilefojing.service.IRSourceUserService;
import com.jilefojing.service.IUserService;
import com.jilefojing.service.base.impl.BaseService;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月21日 00时01分08秒
 */
@Service
public class RSourceUserService extends BaseService<RSourceUser> implements IRSourceUserService {
	@Autowired
	private IRSourceUserDao rSourceUserDao;
	@Autowired
	private IUserService userService;

	@Override
	public List<EnhancedRSourceUser> getEnhanceds(ItemTypeEnumForSource itemType, List<Long> itemIds, UserContext userContext) {
		List<RSourceUser> rSourceUsers = rSourceUserDao.getByItems(itemType, itemIds);
		if (!Detect.notEmpty(rSourceUsers)) {
			return null;
		}
		List<EnhancedRSourceUser> enhancedRSourceUsers = this.convent2Enhanceds(rSourceUsers);
		this.appendEnhancedCommons(enhancedRSourceUsers, userContext);
		return enhancedRSourceUsers;
	}

	@Override
	public List<EnhancedRSourceUser> appendEnhancedUser(List<EnhancedRSourceUser> enhancedRSourceUsers, UserContext userContext) {
		List<Long> userIds = new ArrayList<Long>();
		for (EnhancedRSourceUser _EnhancedRSourceUser : enhancedRSourceUsers) {
			userIds.add(_EnhancedRSourceUser.getEnhancedUser().getId());
		}
		List<EnhancedUser> enhancedUsers = userService.getEnhancedUsers(userIds);
		if (Detect.notEmpty(enhancedUsers)) {
			for (EnhancedRSourceUser _EnhancedRSourceUser : enhancedRSourceUsers) {
				for (EnhancedUser _EnhancedUser : enhancedUsers) {
					if (_EnhancedRSourceUser.getEnhancedUser().getId() == _EnhancedUser.getId()) {
						_EnhancedRSourceUser.setEnhancedUser(_EnhancedUser);
						break;
					}
				}
			}
		}
		return enhancedRSourceUsers;
	}

	@Override
	public List<EnhancedRSourceUser> appendEnhancedCommons(List<? extends BaseEnhanced> enhancedItems, UserContext userContext) {
		@SuppressWarnings("unchecked")
		List<EnhancedRSourceUser> enhancedRSourceUsers = (List<EnhancedRSourceUser>) enhancedItems;
		this.appendEnhancedUser(enhancedRSourceUsers, userContext);
		return enhancedRSourceUsers;
	}

	@Override
	public List<EnhancedRSourceUser> convent2Enhanceds(List<? extends BasePo> pos) {
		@SuppressWarnings("unchecked")
		List<RSourceUser> rSourceUsers = (List<RSourceUser>) pos;
		List<EnhancedRSourceUser> enhancedRSourceUsers = new ArrayList<EnhancedRSourceUser>();
		for (RSourceUser _RSourceUser : rSourceUsers) {
			enhancedRSourceUsers.add(new EnhancedRSourceUser(_RSourceUser));
		}
		return enhancedRSourceUsers;
	}
}
